import { reactive } from 'vue'

type ToastType = 'info' | 'success' | 'warning' | 'error'
export interface ToastItem { id: number; type: ToastType; text: string; duration: number }

const store = reactive({ list: [] as ToastItem[] })
let seed = 1

export function useToast() {
  const remove = (id: number) => {
    const i = store.list.findIndex(t => t.id === id)
    if (i > -1) store.list.splice(i, 1)
  }

  const push = (text: string, type: ToastType = 'info', duration = 2500) => {
    const id = seed++
    store.list.push({ id, type, text, duration })
    window.setTimeout(() => remove(id), duration)
    return id // 可选：返回 id，便于手动关闭
  }

  return {
    store,
    push,
    remove,
    info:    (t: string, d?: number) => push(t, 'info', d),
    success: (t: string, d?: number) => push(t, 'success', d),
    warning: (t: string, d?: number) => push(t, 'warning', d),
    error:   (t: string, d?: number) => push(t, 'error', d),
  }
}
